package com.rybbaby.tss.front.tssAppUserCorrelationBaby.service.imp;

import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssAppUser.entity.TssAppUser;
import com.rybbaby.tss.front.tssAppUser.service.ITssAppUserService;
import com.rybbaby.tss.front.tssAppUserCorrelationBaby.entity.TssAppUserCorrelationBaby;
import com.rybbaby.tss.front.tssAppUserCorrelationBaby.service.ITssAppUserTssAppUserCorrelationBabyService;
import com.rybbaby.tss.front.tssPertainRelation.entity.TssPertainRelation;
import com.rybbaby.tss.front.tssPertainRelation.service.ITssPertainRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

@Service
public class TssAppUserCorrelationBabyServiceImp extends BaseServiceImp<TssAppUserCorrelationBaby> implements ITssAppUserTssAppUserCorrelationBabyService {

    @Autowired
    private ITssAppUserService appUserService;
    @Autowired
    private ITssPertainRelationService pertainRelationService;

    @Override
    public void deleteBaby(List<TssAppUserCorrelationBaby> appUserCorrelationBabies, List<String> familyMemberIds) {
        if (appUserCorrelationBabies != null && !appUserCorrelationBabies.isEmpty()) {
            this.batchDelete(appUserCorrelationBabies);
            if (familyMemberIds != null && !familyMemberIds.isEmpty()) {
                this.pertainRelationService.batchDelete(StringUtils.split(StringUtils.join(familyMemberIds, ","), ","));
            }
        }
    }

    @Override
    public Serializable correlationBaby(String appUserId, Integer relation, String studentId) {
        TssAppUser appUser = this.appUserService.get(appUserId);
        if (appUser != null) {
            //新增关联关系
            TssPertainRelation pertainRelation = new TssPertainRelation();
            pertainRelation.setStudentId(studentId);
            pertainRelation.setPatriarchName(appUser.getName());
            pertainRelation.setAffiliationRelation(relation);
            pertainRelation.setPositionType(2);//监护人
            pertainRelation.setCreateTime(new Timestamp(System.currentTimeMillis()));
            pertainRelation.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            pertainRelation.setContactPhone(appUser.getPhoneNum());
            Serializable familyMemberId = this.pertainRelationService.save(pertainRelation);
            //新增家人
            if (familyMemberId != null) {
                TssAppUserCorrelationBaby tssAppUserCorrelationBaby = new TssAppUserCorrelationBaby();
                tssAppUserCorrelationBaby.setAppUserId(appUserId);
                tssAppUserCorrelationBaby.setStudentId(studentId);
                tssAppUserCorrelationBaby.setFamilyMemberId(familyMemberId.toString());
                Serializable newId = this.save(tssAppUserCorrelationBaby);
                return newId;
            }
        }
        return null;
    }
}
